package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.bo.IotUpkeepProjectBo;
import org.dromara.iot.domain.vo.IotUpkeepProjectVo;
import org.dromara.iot.service.IIotUpkeepProjectService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电梯保养项目
 *
 * @author lsl
 * @date 2024-06-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/upkeepProject")
public class IotUpkeepProjectController extends BaseController {

    private final IIotUpkeepProjectService iotUpkeepProjectService;

    /**
     * 查询电梯保养项目列表
     */
    @SaCheckPermission("iot:upkeepProject:list")
    @GetMapping("/list")
    public TableDataInfo<IotUpkeepProjectVo> list(IotUpkeepProjectBo bo, PageQuery pageQuery) {
        return iotUpkeepProjectService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯保养项目列表
     */
    @SaCheckPermission("iot:upkeepProject:export")
    @Log(title = "电梯保养项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotUpkeepProjectBo bo, HttpServletResponse response) {
        List<IotUpkeepProjectVo> list = iotUpkeepProjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯保养项目", IotUpkeepProjectVo.class, response);
    }

    /**
     * 获取电梯保养项目详细信息
     *
     * @param projectId 主键
     */
    @SaCheckPermission("iot:upkeepProject:query")
    @GetMapping("/{projectId}")
    public R<IotUpkeepProjectVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable Long projectId) {
        return R.ok(iotUpkeepProjectService.queryById(projectId));
    }

    /**
     * 新增电梯保养项目
     */
    @SaCheckPermission("iot:upkeepProject:add")
    @Log(title = "电梯保养项目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotUpkeepProjectBo bo) {
        return toAjax(iotUpkeepProjectService.insertByBo(bo));
    }

    /**
     * 修改电梯保养项目
     */
    @SaCheckPermission("iot:upkeepProject:edit")
    @Log(title = "电梯保养项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotUpkeepProjectBo bo) {
        return toAjax(iotUpkeepProjectService.updateByBo(bo));
    }

    /**
     * 删除电梯保养项目
     *
     * @param projectIds 主键串
     */
    @SaCheckPermission("iot:upkeepProject:remove")
    @Log(title = "电梯保养项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] projectIds) {
        return toAjax(iotUpkeepProjectService.deleteWithValidByIds(List.of(projectIds), true));
    }
}
